package com.movitech.mobile.cz.base.purchase.bizcontroller;

import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.purchase.bizservice.PStrategicPurchaseService;
import com.movitech.mobile.cz.base.purchase.bizvo.DealRequestVO;
import com.movitech.mobile.cz.base.purchase.bizvo.PStrategicContactListParamVo;
import com.movitech.mobile.cz.base.purchase.bizvo.PspOverallReqVO;
import com.movitech.mobile.cz.base.purchase.entity.PSpPlanConfirmProc;
import com.movitech.mobile.cz.base.purchase.entity.PStrategicPurchaseContact;
import com.movitech.mobile.cz.base.purchase.entity.ext.*;
import com.movitech.mobile.cz.base.purchase.service.PStrategicPurchaseContactService;
import com.movitech.mobile.cz.base.purchase.service.PStrategicPurchaseRequiredService;
import com.movitech.mobile.cz.base.supplier.bizvo.OverAllConfirmHistoryRequestVO;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSHistoryComment;
import com.movitech.mobile.cz.base.supplier.service.SHistoryCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by robie.cao on 2018/5/14.
 */
@RestController
@RequestMapping("/v1/pStrategicPurchase")
@Api(value = "战略采购相关接口", description = "战略采购相关接口")
public class PStrategicPurchaseController {

    @Autowired
    private PStrategicPurchaseService pStrategicPurchaseService;

    @Autowired
    private PStrategicPurchaseContactService pStrategicPurchaseContactService;

    @Autowired
    private PStrategicPurchaseRequiredService pStrategicPurchaseRequiredService;

    @Autowired
    private SHistoryCommentService sHistoryCommentService;


    @PostMapping("/pStrategicPurchaseRequireList")
    @ApiOperation(
            value = "战略采购需求列表",
            notes = "战略采购需求列表"
    )
    public RestReturnResult pStrategicPurchaseRequireList(
            @RequestBody StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest) {
        RestReturnResult<Object> hResult  = RestReturnResult.success();
        PageCode page = pStrategicPurchaseService.getPStrategicPurchaseRequireList(strategicPurchaseRequiredFindRequest);
        hResult.setData(page);
        return hResult;
    }


    @GetMapping("/deletePStrategicRequireDetail/{id}")
    @ApiOperation(value = "删除战略采购需求", notes = "删除战略采购需求")
    public RestReturnResult deletePStrategicPurchasePlan(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        pStrategicPurchaseService.deleteDetailById(id);
        return restResult;
    }

    @PostMapping("/pStrategicPurchasePlanList")
    @ApiOperation(
            value = "战略采购规划列表",
            notes = "战略采购规划列表"
    )
    public RestReturnResult getPStrategicPurchasePlanList(
            @RequestBody StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest) {
        RestReturnResult<Object> hResult  = RestReturnResult.success();
        PageCode page = pStrategicPurchaseService.getPStrategicPurchasePlanList(strategicPurchaseRequiredFindRequest);
        hResult.setData(page);
        return hResult;
    }

    @PostMapping("/getPStrategicPurchasePlan")
    @ApiOperation(value = "战略采购规划获取接口", notes = "战略采购规划获取接口")
    public RestReturnResult addPStrategicPurchaseRequired(@RequestBody StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest) {
        RestReturnResult restResult = RestReturnResult.success();
        ExtPStrategicPurchasePlan extPStrategicPurchasePlan = pStrategicPurchaseService.getByReqEntity(strategicPurchaseRequiredFindRequest);
        restResult.setData(extPStrategicPurchasePlan);
        return restResult;
    }


    @PostMapping("/saveOrUpdatePStrategicPurchasePlan")
    @ApiOperation(value = "新增/修改战略采购规划", notes = "新增/修改战略采购规划")
    public RestReturnResult saveOrUpdatePStrategicPurchasePlan(@RequestBody ExtPStrategicPurchasePlan extPStrategicPurchasePlan) {
        RestReturnResult restResult = RestReturnResult.success();
        if(!pStrategicPurchaseService.saveOrUpdatePStrategicPurchasePlan(extPStrategicPurchasePlan)){
            restResult = RestReturnResult.error();
        }
        restResult.setData(extPStrategicPurchasePlan);
        return restResult;
    }



    @PostMapping("/pSpOverallPlanList")
    @ApiOperation(value = "战略采购年度总规划列表查询接口", notes = "战略采购年度总规划列表查询接口")
    public RestReturnResult pSpOverallPlanList(@RequestBody PStrategicPurchasePlanReq pStrategicPurchasePlanReq) {
        RestReturnResult<Object> hResult  = RestReturnResult.success();
        PageCode page = pStrategicPurchaseService.getPSpOverallPlanList(pStrategicPurchasePlanReq);
        hResult.setData(page);
        return hResult;
    }

    @GetMapping("/getPSpOverallPlan/{id}")
    @ApiOperation(value = "战略采购年度总规划ID获取接口", notes = "战略采购年度总规划ID获取接口")
    public RestReturnResult getPSpOverallPlan(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        ExtPSpOverallPlanDetail extPSpOverallPlanDetail = pStrategicPurchaseService.getPSpOverallPlan(id);
        restResult.setData(extPSpOverallPlanDetail);
        return restResult;
    }

    @PostMapping("/generatePSpOverallPlan")
    @ApiOperation(value = "生成战略采购年度总规划", notes = "生成战略采购年度总规划")
    public RestReturnResult generatePSpOverallPlan(@RequestBody PspOverallReqVO pspOverallReqVO) {
        RestReturnResult restResult = RestReturnResult.success();
        if(!pStrategicPurchaseService.generatePSpOverallPlan(pspOverallReqVO)){
            restResult = RestReturnResult.error();
        }
        return restResult;
    }


    @PostMapping("/saveOrUpdatePSpOverallPlan")
    @ApiOperation(value = "修改战略采购年度总规划", notes = "修改战略采购年度总规划")
    public RestReturnResult saveOrUpdatePSpOverallPlan(@RequestBody ExtPSpOverallPlanDetail pspOverallReqVO) {
        RestReturnResult restResult = RestReturnResult.success();
        if(!pStrategicPurchaseService.saveOrUpdatePSpOverallPlan(pspOverallReqVO)){
            restResult = RestReturnResult.error();
        }
        return restResult;
    }

    @PostMapping("/launchOverallPlanConfirm")
    @ApiOperation(value = "发起年度总规划确认", notes = "发起年度总规划确认")
    public RestReturnResult launchOverallPlanConfirm(@RequestBody PspOverallReqVO pspOverallReqVO) {
        RestReturnResult restResult = RestReturnResult.success();
        pStrategicPurchaseService.launchOverallPlanConfirm(pspOverallReqVO);
        return restResult;
    }

    @PostMapping("/overallPlanConfirmDealDtl")
    @ApiOperation(value = "年度总规划代办详情", notes = "年度总规划代办详情")
    public RestReturnResult overallPlanConfirmDealDtl(@RequestBody PStrategicPurchasePlanReq pStrategicPurchasePlanReq) {
        RestReturnResult restResult = RestReturnResult.success();
        restResult.setData(pStrategicPurchaseService.overallPlanConfirmDealDtl(pStrategicPurchasePlanReq));
        return restResult;
    }

    @PostMapping("/confirmOverallPlan")
    @ApiOperation(value = "年度总规划代办确认", notes = "年度总规划代办确认")
    public RestReturnResult confirmOverallPlan(@RequestBody DealRequestVO dealRequestVO) {
        RestReturnResult restResult = RestReturnResult.success();
        pStrategicPurchaseService.confirmOverallPlan(dealRequestVO);
        return restResult;
    }

    @PostMapping("/searchConfirmHistoryList")
    @ApiOperation(
            value = "年度总规划代办确认历史",
            notes = "年度总规划代办确认历史",
            response = PSpPlanConfirmProc.class
    )
    public RestReturnResult searchConfirmHistoryList(
            @RequestBody OverAllConfirmHistoryRequestVO overAllConfirmHistoryRequestVO) {
        RestReturnResult restResult = RestReturnResult.success();
        Page<ExtSHistoryComment> page = new Page<ExtSHistoryComment>();
        page.setCurrent(overAllConfirmHistoryRequestVO.getPageNumber());
        page.setSize(overAllConfirmHistoryRequestVO.getPageSize());
        page = sHistoryCommentService.findConfirmHistory(page,overAllConfirmHistoryRequestVO);
        restResult.setData(page);
        return restResult;
    }



    @PostMapping("/purchaseItemList")
    @ApiOperation(
            value = "战略招标项列表",
            notes = "战略招标项列表"
    )
    public RestReturnResult<Page<ExtPStrategicPurchaseContact>> searchList(@ApiParam(value = "采购列表参数",required = true) @RequestBody PStrategicContactListParamVo pStrategicContactListParamVo) {
        RestReturnResult restResult = RestReturnResult.success();
        Page<ExtPStrategicPurchaseContact> page = new Page<>();
        page.setCurrent(pStrategicContactListParamVo.getPageRequestVO().getPageNumber());
        page.setSize(pStrategicContactListParamVo.getPageRequestVO().getPageSize());
        pStrategicPurchaseContactService.findExtList(page,pStrategicContactListParamVo.getFlowFormListReqVO());
        restResult.setData(page);
        return restResult;
    }

    @PostMapping("/saveOrUpdatePurchaseContact")
    @ApiOperation(value = "新增/修改战略招标项", notes = "新增/修改战略招标项")
    public RestReturnResult saveOrUpdatePurchaseContact(@RequestBody PStrategicPurchaseContact extPStrategicPurchaseContact) {
        RestReturnResult restResult = RestReturnResult.success();
        if(!pStrategicPurchaseService.saveOrUpdatePurchaseContact(extPStrategicPurchaseContact)){
            restResult = RestReturnResult.error();
        }
        restResult.setData(extPStrategicPurchaseContact);
        return restResult;
    }

}
